<template>
    <div class="personal">
        <div class="header">
            <!-- 头部左侧 -->
            <div class="left" @click="$router.back()">取消</div>
            <!-- 头部标题 -->
            <div class="title">信息编辑</div>
            <!-- 头部右侧 -->
            <div class="right" @click="updated">保存</div>
        </div>
        <!-- 头像 -->
        <div class="head" @click="showBottom = true">
            <div class="head_img">
                <img :src="headerImg || userInfo.header_img" alt="" />
            </div>
            <p>更改头像</p>
        </div>

        <!-- 弹出层 -->
        <nut-popup
            position="bottom"
            :style="{ height: '70%' }"
            v-model:visible="showBottom"
        >
            <img
                v-for="(item, index) in imgList"
                :key="index"
                :src="item"
                alt=""
                @click="select(item)"
            />
        </nut-popup>

        <div class="info_item">
            <span>昵称</span
            ><input
                type="text"
                v-model="userName"
                :placeholder="this.userInfo.name || '获取用户名失败'"
            />
        </div>
        <div class="info_item"><span>会员ID</span><i>69193216412001</i></div>

        <!-- 提示框 -->
        <nut-dialog
            teleport="#app"
            title="提示"
            content="保存成功"
            v-model:visible="visible"
        >
            <template #footer>
                <nut-button
                    @click="$router.back()"
                    size="small"
                    color="#C1AB96"
                    plain
                    >确定</nut-button
                >
            </template>
        </nut-dialog>
    </div>
</template>

<script>
import { updateUserInfo } from "../api/user";
export default {
  name: "editPersonalInfoView",
  data () {
        return {
            userName: "",
            headerImg: "",
            showBottom: false,
            imgList: [
                "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fblog%2F202108%2F22%2F20210822070705_cfd6b.thumb.1000_0.jpg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1660630369&t=26c48d5f6b0f8ed346b9560ca7e2d1a1",
                "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fhbimg.b0.upaiyun.com%2F446e08d3f95f7c54cb720ab4bb0fa30cb382faff10263-afWjr9_fw658&refer=http%3A%2F%2Fhbimg.b0.upaiyun.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1660794755&t=27353d1aea8d03ff73ebdbee079f6441",
                "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F201912%2F24%2F20191224161919_fxeso.jpg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1660632473&t=8612062d57354514898b4918aac4b619",
            ],
            visible: false,
        };
    },
  
    methods: {
        // 更新个人信息
        async updated() {
            // 输入用户名为空，则发送原来的名字，否则使用新名字
            this.userName = !this.userName ? this.userInfo.name : this.userName;
            // 输入头像地址为空，则发送原来的图片，否则使用新图片
            this.headerImg = !this.headerImg
                ? this.userInfo.header_img
                : this.headerImg;
            // 传入用户名
            const { data } = await updateUserInfo(this.userInfo.id, {
                password: this.userInfo.password,
                phone: this.userInfo.phone,
                name: this.userName,
                header_img: this.headerImg,
            });
            this.visible = true;
            console.log(data.result);
        },
        select(item) {
            this.headerImg = item;
            this.showBottom = false;
        },
    },
    computed: {
        // 用户信息对象
        userInfo() {
            return this.$store.state.userInfo;
        },
    },
    mounted() {
        // 请求用户信息
        this.$store.dispatch("getUserInfo");
    },
};
</script>

<style lang="scss" scoped>
.personal {
    height: 100vh;
    background-color: #fff;
}
.header {
    box-sizing: border-box;
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
    padding: 2.667vw 3.733vw;
    height: 17.067vw;
    font-size: 4.267vw;
    font-weight: 700;
    background-color: #eae9e5;

    .left {
        font-size: 3.2vw;
        width: 13.333vw;
        font-weight: 400;
        text-align: center;
    }
    .right {
        width: 13.333vw;
        font-size: 3.2vw;
        font-weight: 400;
        text-align: center;
    }
}
.head {
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 128px;
    margin: 0 auto;
    .head_img {
        border-radius: 50%;
        width: 120px;
        height: 120px;
        overflow: hidden;
        margin: 16px 0 10px;
        img {
            width: 100%;
            height: 100%;
        }
    }
    p {
        font-size: 12px;
        color: #c1ab96;
    }
}
.info_item {
    display: flex;
    align-items: center;
    font-size: 3.733vw;
    height: 13.333vw;
    margin: 0 16px;
    border-bottom: 0.267vw solid rgb(232, 231, 231);
    span {
        width: 18.133vw;
    }
    i {
        color: #999;
    }
    input {
        font-size: 3.2vw;
        flex: 1;
        border: 0;
        outline: none;
    }
}
// 弹出层
.nut-popup {
    img {
        width: 100px;
        height: 100px;
        background-color: #000;
        margin: 5px;
    }
}
</style>